﻿using Microsoft.Extensions.Configuration;
using OConf.Entity;
using OConf.Model.Common;
using System;
using System.Linq;
using System.Linq.Expressions;
namespace OConf.Service
{
    public class LoginLogService
    {
        private readonly IConfiguration _configuration;

        public LoginLogService(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public bool AddEntity(LoginLogEntity entity)
        {
            bool isSuccess = false;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                db.LoginLog.Add(entity);
                isSuccess = db.SaveChanges() > 0;
            }
            return isSuccess;
        }

        public ReturnTableData<LoginLogEntity> QueryPage(int pageIndex, int pageSize, Expression<Func<LoginLogEntity, bool>> whereLambda)
        {
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                ReturnTableData<LoginLogEntity> result = new ReturnTableData<LoginLogEntity>();
                result.Total = db.LoginLog.Where(whereLambda).Count();

                if (result.Total > 0)
                {
                    result.List = db.LoginLog.Where(whereLambda)
                        .OrderByDescending(o => o.CreateTime)
                        .Skip(pageSize * (pageIndex - 1))
                        .Take(pageSize).ToList();
                }
                return result;
            }
        }
    }
}
